package com.bootdo.system.controller;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.system.domain.RoleDO;
import com.bootdo.system.domain.TbOrder;
import com.bootdo.system.domain.TbStock;
import com.bootdo.system.domain.TbSvip;
import com.bootdo.system.domain.TbViporder;
import com.bootdo.system.domain.TbWxuser;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.OrderService;
import com.bootdo.system.service.ProductService;
import com.bootdo.system.service.RoleService;
import com.bootdo.system.service.WxUserService;

@RequestMapping("/order")
@Controller
public class OrderController {
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	RoleService roleService;
	
	@Autowired
	private WxUserService wxUserService;
	
	@Autowired
	private ProductService productService;
	
	String prefix = "system/order";
	
	@GetMapping()
	String product() {
		return prefix + "/order";
	}
	
	@GetMapping("ddyj")
	String ddyj() {
		return prefix + "/ddyj";
	}
	
	@GetMapping("km")
	String orderMt() {
		return prefix + "/orderKm";
	}
	
	@ResponseBody
	@RequestMapping("/list")
	public PageUtils gzhConfigList(@RequestParam Map<String, Object> params) {
		UserDO user = ShiroUtils.getUser();
		if(user.getUserId() != 1L) {
			List<RoleDO> list2 = roleService.getRoleByUserId(user.getUserId());
			boolean falg=false;
			for (RoleDO role : list2) {
				if(role.getRoleId() == 59L) {
					falg=true;
				}
			}
			if(falg) {//公司数据权限
				params.put("deptId", user.getDeptId());
			}else {
				params.put("userId", user.getUserId());
			}
		}
		Query query = new Query(params);
		 List<TbOrder> list = orderService.list(query);
		List<TbOrder> res=new ArrayList<TbOrder>();
		 for (TbOrder tbOrder : list) {
			 String openid = tbOrder.getOpenid();
			 TbWxuser userById = wxUserService.getUserById(openid);
			 if(userById != null) {
				 tbOrder.setWxInfo(userById.getNickname()+" ( ID : "+userById.getId()+" ) ");
			 }
			 res.add(tbOrder);
		}
		 int count = orderService.count(query);
		 PageUtils pageUtil = new PageUtils(res, count);
		 return pageUtil;
	}
	
	@ResponseBody
	@RequestMapping("/listDdyj")
	public PageUtils listDdyj(@RequestParam Map<String, Object> params) {
		UserDO user = ShiroUtils.getUser();
		String type = (String) params.get("type");
		if(user.getUserId() != 1L) {
			params.put("sid",user.getUserId() );
		}
		if(("1").equals(type)) {
			Query query = new Query(params);
			 List<Map<String, Object>> list = orderService.list5(query);
			 for (Map<String, Object> map : list) {
				 Double profit2 = (Double) map.get("profit2");
				 Double direct = (Double) map.get("direct");
				 map.put("profit2", getDouble2String2(profit2+""));
				 map.put("direct", getDouble2String2(direct+""));
			}
			 int count = orderService.count5(query);
			 PageUtils pageUtil = new PageUtils(list, count);
			 return pageUtil;
		}else if(("2").equals(type)) {
			Query query = new Query(params);
			 List<Map<String, Object>> list = orderService.list6(query);
			 for (Map<String, Object> map : list) {
				 Double profit2 = (Double) map.get("profit2");
				 Double direct = (Double) map.get("direct");
				 map.put("profit2", getDouble2String2(profit2+""));
				 map.put("direct", getDouble2String2(direct+""));
			}
			 int count = orderService.count6(query);
			 PageUtils pageUtil = new PageUtils(list, count);
			 return pageUtil;
		}
		return null;
	}
	private Double getDouble2String(String num) {
		if(StringUtils.isNoneBlank(num)) {
			Double cny = Double.parseDouble(num);
			DecimalFormat df = new DecimalFormat("0.00000"); 
			String CNY = df.format(cny);
			Double cny1 = Double.parseDouble(CNY);
			return cny1;
		}
		return 0.00;
	}
	
	private Double getDouble2String2(String num) {
		if(StringUtils.isNoneBlank(num) && !("null").equals(num)) {
			Double cny = Double.parseDouble(num);
			DecimalFormat df = new DecimalFormat("0.00"); 
			String CNY = df.format(cny);
			Double cny1 = Double.parseDouble(CNY);
			return cny1;
		}
		return 0.00;
	}
	
	@ResponseBody
	@RequestMapping("/listKm")
	public PageUtils listKm(@RequestParam Map<String, Object> params) {
		UserDO user = ShiroUtils.getUser();
		if(user.getUserId() != 1L) {
			List<RoleDO> list2 = roleService.getRoleByUserId(user.getUserId());
			boolean falg=false;
			for (RoleDO role : list2) {
				if(role.getRoleId() == 59L) {
					falg=true;
				}
			}
			if(falg) {//公司数据权限
				params.put("deptId", user.getDeptId());
			}else {
				params.put("userId", user.getUserId());
			}
		}
		Query query = new Query(params);
		List<TbViporder> list=	orderService.listKm(query);
		List<TbViporder> res=new ArrayList<TbViporder>();
		for (TbViporder tbViporder : list) {
			String openid = tbViporder.getOpenid();
			TbWxuser userById = wxUserService.getUserById(openid);
			Integer id = userById.getId();
			tbViporder.setWxId(userById.getNickname()+" ( ID : "+id+" ) ");
			String productId = tbViporder.getProductId();
			TbSvip byStoId = productService.getByStoId(Integer.valueOf(productId));
			if(byStoId != null) {
				String productclass = byStoId.getProductclass();
				if(("mt").equals(productclass)) {
					productclass="美团";
				}else {
					productclass="饿了么";
				}
				tbViporder.setProInfo(productclass);
			}
			res.add(tbViporder);
		}
		int count= orderService.countKm(query);
		 PageUtils pageUtil = new PageUtils(res, count);
		 return pageUtil;
	}
	
	
	@GetMapping("/edit/{id}")
	String edit(Model model,@PathVariable("id") Integer id) {
		TbOrder us = orderService.getById(id);
		model.addAttribute("us", us);
		return prefix+"/edit";
	}
	
	
	@PostMapping("/update")
	@ResponseBody
	R update(TbOrder table) {
		if (orderService.insert2update(table) > 0) {
			return R.ok();
		}
		return R.error();
	}
	
	
	
	@PostMapping("/remove")
	@ResponseBody
	R remove(Integer id) {
		if (orderService.remove(id) > 0) {
			return R.ok();
		}
		return R.error();
	}
	
	@PostMapping("/batchRemove")
	@ResponseBody
	R batchRemove(@RequestParam("ids[]") Integer[] ids) {
		int r = orderService.batchremove(ids);
		if (r > 0) {
			return R.ok();
		}
		return R.error();
	}
	
	@PostMapping("/removeKm")
	@ResponseBody
	R removeKm(Integer id) {
		if (orderService.removeKm(id) > 0) {
			return R.ok();
		}
		return R.error();
	}
	
	@PostMapping("/batchRemoveKm")
	@ResponseBody
	R batchRemoveKm(@RequestParam("ids[]") Integer[] ids) {
		int r = orderService.batchremoveKm(ids);
		if (r > 0) {
			return R.ok();
		}
		return R.error();
	}
}
